package com.controller;


import com.entity.Department;
import com.entity.Employee;
import com.service.DepartmentService;
import com.service.EmployeeService;
import com.utils.AesUtils;
import com.utils.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *  员工前端控制器
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    //用户管理：分页和下拉框查询
    @RequestMapping("/list")
    public String list(PageObject pageObject, Employee employee, Model model){
        model.addAttribute("departmentList", departmentService.list());
        model.addAttribute("page", employeeService.list(pageObject,employee));
        model.addAttribute("employee", employee);
        return "employeeList";
    }

    //用户管理：设置状态
    @PostMapping("/setStatus")
    @ResponseBody
    public String setStatus(Integer status,Integer id){
        boolean setStatus = this.employeeService.setStatus(status, id);
        if(setStatus) return "success";
        return "fail";
    }

    //用户管理：重置密码
    @PostMapping("/resetPassword")
    @ResponseBody
    public String resetPassword(Integer id){
        boolean resetPassword = this.employeeService.resetPassword(id);
        if(resetPassword) return "success";
        return "fail";
    }

    //用户管理：添加页初始化
    @GetMapping("/add")
    public String add(Model model){
        model.addAttribute("departmentList", departmentService.list());
        return "employeeAdd";
    }

    //用户管理：添加功能保存
    @PostMapping("/save")
    public String save(Employee employee){
        employee.setStatus(1);
        employee.setPassword(AesUtils.encryptStr("123456",AesUtils.SECRETKEY));
        boolean save = this.employeeService.save(employee);
        if(save) return "redirect:/employee/list";
        return "employeeAdd";
    }

    //用户管理：编辑员工-Id查询
    @GetMapping("/findById")
    public String findById(Integer id,Model model){
        model.addAttribute("employee", this.employeeService.getById(id));
        model.addAttribute("departmentList", this.departmentService.list());
        return "employeeEdit";
    }

    //用户管理：编辑员工
    @PostMapping("/update")
    public String update(Employee employee){
        employee.setStatus(1);
        employee.setPassword(AesUtils.encryptStr("123456",AesUtils.SECRETKEY));
        boolean save = this.employeeService.updateById(employee);
        if(save) return "redirect:/employee/list";
        return "employeeAdd";
    }

    //用户管理：删除员工
    @PostMapping("/delete")
    @ResponseBody
    public String delete(Integer id){
        boolean remove = employeeService.removeById(id);
        if(remove) return "success";
        return "fail";
    }


}

